// pages/index/component/bargainProduct/bargainProductRow.js
import {
  list,getOrderList
} from '../../../../api/bargainBuy';
const util = require('../../../../utils/util')

const app = getApp()
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    info: {
      type: Object,
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    products: [],
    heads: [],
    pageNum: 0,
    pageSize: 20,
    productType:false,
  },

  /**
   * 组件的方法列表
   */
  lifetimes: {
    attached() {
      if(this.data.info.rows!=''||this.data.info.rows!=null){
        this.data.pageSize=this.data.info.rows
        this.setData({
          pageSize:this.data.pageSize
        })
      }
      this.loadProduct()
    }
  },
  pageLifetimes: {
    show: function () {
      // 页面被展示
      this.data.pageNum=0
      this.loadProduct()
    },
  },
  methods: {
    toProduct(data) {
      wx.navigateTo({
        url: '/pages/goods/bargain/barginDetails?id=' + data.currentTarget.dataset.id
      })
    },
    loadProduct() {
      var _this = this
      _this.data.pageNum += 1
      list({
        mchId: app.globalData.mchId,
        pageSize: _this.data.pageSize,
        pageNum: _this.data.pageNum,
      }).then(res => {
       
        if (_this.data.pageNum == 1) {
          _this.data.products = []
        }
        if (res.data.data.length < 20) {
          _this.data.productType = true
        }
        res.data.data.forEach(item => {
          item.point=(item.point/100).toFixed(2)
          this.data.heads=[]
          if (item.orderList) {
            if (item.orderList.length>2) {
              if (!item.orderList[0].avatar) {
                item.orderList[0].avatar='http://rzico.oss-cn-shenzhen.aliyuncs.com/img/hhahahah.jpg'
              }else if (!item.orderList[1].avatar) {
                item.orderList[1].avatar='http://rzico.oss-cn-shenzhen.aliyuncs.com/img/hhahahah.jpg'
              }
                  this.data.heads.push(item.orderList[0])
                  this.data.heads.push(item.orderList[1])
                  item.orderList[2].avatar='http://rzico.oss-cn-shenzhen.aliyuncs.com/img/more.png'
                  this.data.heads.push(item.orderList[2])
                  item.orderList=this.data.heads
            }else{
              item.orderList.forEach(item1=>{
                if (!item1.avatar) {
                  item1.avatar='http://rzico.oss-cn-shenzhen.aliyuncs.com/img/hhahahah.jpg'
                }
              })
            }
          }
          item.meta = JSON.parse(item.meta)
          item.thumbnail = util.thumbnail(item.thumbnail, 346, 350)
          _this.data.products.push(item)
        })
        this.setData({
          productType:this.data.productType,
          heads:this.data.heads,
          products: this.data.products
        })
      })
    },
       // +++++++++++上拉加载+++++++++
       onReachBottom: function () {
        this.loadProduct()
      },
      downRefresh: function () {
        this.data.productList = []
        // this.data.pageSize = 20
        this.data.pageNum = 0
        this.loadProduct()
        this.setData({
          productList: this.data.productList,
          pageNum: this.data.pageNum
          // pageSize: this.data.pageSize
        })
      },
  }
})
